package com.zxl.always.on.display.tools;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.util.Log;

import com.zxl.always.on.display.ApplicationHelper;

public class ResourceUtils {
    public static final float FONT_DEFAULT_SCALE = 1.0F;
    public static final float FONT_MAX_SCALE = 1.2F;
    public static final float FONT_MIN_SCALE = 1.0F;
    private static final String TAG = ResourceUtils.class.getSimpleName();
    private static float sScreenDensityRate = 0.0F;
    private static float sScreenDensityRateWQHD640Dpi = 0.0F;
    private static float sScreenSizeRate = 0.0F;

    public static int getDimensionPixelOffset(int paramInt) {
        float f = getScreenDensityRate();
        if (f == 1.0F) {
            return getResources().getDimensionPixelOffset(paramInt);
        }
        return Math.round(f * getResources().getDimensionPixelOffset(paramInt));
    }

    public static int getDimensionPixelOffset(@NonNull TypedArray paramTypedArray, int paramInt1, int paramInt2) {
        float f = getScreenDensityRate();
        if (f == 1.0F) {
            return paramTypedArray.getDimensionPixelOffset(paramInt1, paramInt2);
        }
        return Math.round(f * paramTypedArray.getDimensionPixelOffset(paramInt1, paramInt2));
    }

    public static int getDimensionPixelSize(int paramInt) {
        float f = getScreenDensityRate();
        if (f == 1.0F) {
            return getResources().getDimensionPixelSize(paramInt);
        }
        return Math.round(f * getResources().getDimensionPixelSize(paramInt));
    }

    public static int getDimensionPixelSize(@NonNull TypedArray paramTypedArray, int paramInt1, int paramInt2) {
        float f = getScreenDensityRate();
        if (f == 1.0F) {
            return paramTypedArray.getDimensionPixelSize(paramInt1, paramInt2);
        }
        return Math.round(f * paramTypedArray.getDimensionPixelSize(paramInt1, paramInt2));
    }

    public static float getFontScale(Context paramContext) {
        return Math.max(1.0F, Math.min(1.2F, paramContext.getResources().getConfiguration().fontScale));
    }

    public static Size getFullScreenSize() {
        DisplayMetrics localDisplayMetrics = getResources().getDisplayMetrics();
        return new Size(localDisplayMetrics.widthPixels, localDisplayMetrics.heightPixels);
    }
//
//    public static Point getPhysicalScreenSize() {
//        IWindowManagerReflection localIWindowManagerReflection = ReflectionContainer.getIWindowManagerStub().asInterface(ReflectionContainer.getServiceManager().checkService("window"));
//        Point localPoint = new Point();
//        localPoint.x = 0;
//        localPoint.y = 0;
//        if (localIWindowManagerReflection != null) {
//            localIWindowManagerReflection.getInitialDisplaySize(0, localPoint);
//            Log.d(TAG, "getPhysicalScreenSize [x, y] = [" + localPoint.x + ", " + localPoint.y + "]");
//        }
//        if ((localPoint.x == 0) || (localPoint.y == 0)) {
//            localPoint.x = ((int) 1440.0F);
//            localPoint.y = ((int) 2960.0F);
//            Log.d(TAG, "getPhysicalScreenSize [x, y] = [" + localPoint.x + ", " + localPoint.y + "] (Using default value)");
//        }
//        return localPoint;
//    }

    private static Resources getResources() {
        return ApplicationHelper.getContext().getResources();
    }

    public static float getScreenDensityRate() {
        if (getResources() != null) {
            DisplayMetrics localDisplayMetrics = getResources().getDisplayMetrics();
            float f1 = localDisplayMetrics.density;
            float f2 = localDisplayMetrics.densityDpi;
            float f3 = getScreenSizeRateFromWQHD();
            float f4 = f3 * f2;
            float f5 = f3 * f1;
            float f6;
            if (f4 > 640.0F) {
                f6 = 640.0F / f4;
            } else if (f4 < 560.0F) {
                f6 = 560.0F / f4;
            } else {
                f6 = 1.0F;
            }
            if (Float.compare(f6, sScreenDensityRate) != 0) {
                sScreenDensityRate = f6;
                Log.d(TAG, "getScreenDensityRate: currentDpi: " + f2 + " currentDensity: " + f1 + " scaledDpi: " + f4 + " scaledDensity: " + f5 + " sScreenDensityRate: " + f6);
            }
            return sScreenDensityRate;
        }
        return 1.0F;
    }

    public static float getScreenDensityRateForWQHD640DpiStyle() {
        if (getResources() != null) {
            DisplayMetrics localDisplayMetrics = getResources().getDisplayMetrics();
            float f1 = localDisplayMetrics.density;
            float f2 = localDisplayMetrics.densityDpi;
            float f3 = getScreenSizeRateFromWQHD();
            float f4 = f3 * f2;
            float f5 = f3 * f1;
            float f6 = 640.0F / f4;
            if (Float.compare(f6, sScreenDensityRateWQHD640Dpi) != 0) {
                sScreenDensityRateWQHD640Dpi = f6;
                Log.d(TAG, "getScreenDensityRateForWQHD640DpiStyle: currentDpi: " + f2 + " currentDensity: " + f1 + " scaledDpi: " + f4 + " scaledDensity: " + f5 + " sScreenDensityRate: " + f6);
            }
            return sScreenDensityRateWQHD640Dpi;
        }
        return 1.0F;
    }

    public static float getScreenSizeRate() {
        float f1 = 1.0F;
        Resources localResources = getResources();
        if (localResources != null) {
            DisplayMetrics localDisplayMetrics = localResources.getDisplayMetrics();
            float f2 = localDisplayMetrics.density;
            float f3 = localDisplayMetrics.densityDpi;
            float f4 = localDisplayMetrics.scaledDensity;
            float f5 = Math.min(localDisplayMetrics.widthPixels, localDisplayMetrics.heightPixels);
            float f6 = Math.max(localDisplayMetrics.widthPixels, localDisplayMetrics.heightPixels);
            if (1440.0F >= f5) {
                f1 = f5 / 1440.0F;
            }
            if (Float.compare(f1, sScreenSizeRate) != 0) {
                sScreenSizeRate = f1;
            }
            f1 = sScreenSizeRate;
        }
        return f1;
    }

//    public static float getScreenSizeRateFromPhysical() {
//        float f1 = 1.0F;
//        Resources localResources = getResources();
//        if (localResources != null) {
//            DisplayMetrics localDisplayMetrics = localResources.getDisplayMetrics();
//            float f2 = Math.min(localDisplayMetrics.widthPixels, localDisplayMetrics.heightPixels);
//            IWindowManagerReflection localIWindowManagerReflection = ReflectionContainer.getIWindowManagerStub().asInterface(ReflectionContainer.getServiceManager().checkService("window"));
//            Point localPoint = new Point();
//            float f3 = 1440.0F;
//            if (localIWindowManagerReflection != null) {
//                localIWindowManagerReflection.getInitialDisplaySize(0, localPoint);
//                f3 = localPoint.x;
//                if (Utils.ENG_MODE) {
//                    Log.d(TAG, "getScreenSizeRateFromPhysical [x, y] = [" + localPoint.x + ", " + localPoint.y + "]");
//                }
//            }
//            if (f3 >= f2) {
//                f1 = f3 / f2;
//            }
//        }
//        return f1;
//    }

    private static float getScreenSizeRateFromWQHD() {
        Resources localResources = getResources();
        if (localResources != null) {
            DisplayMetrics localDisplayMetrics = localResources.getDisplayMetrics();
            return 1440.0F / Math.min(localDisplayMetrics.widthPixels, localDisplayMetrics.heightPixels);
        }
        return 1.0F;
    }
}
